<template>
  <div>
    <!--搜索表单设置-->
    <div style="margin-bottom: 20px">
      <!--v-model绑定数据,@click绑定点击事件-->
      <el-input  style="width: 240px;" placeholder="请输入用户名" v-model="params.username"></el-input>
      <el-input  style="width: 240px; padding: 5px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
      <el-input  style="width: 240px; padding: 5px" placeholder="请输入邮箱" v-model="params.email"></el-input>
      <el-button class="el-icon-search" type="primary" @click="load"><span>搜索</span></el-button>
      <el-button class="el-icon-refresh-right" type="warning" @click="reset"><span>重置</span></el-button>
    </div>
    <!--定义一个表格, 绑定数据源tabledata,stripe为表格边框-->
    <el-table :data="tableData" stripe>
      <el-table-column prop="id" label="编号" ></el-table-column>,
      <el-table-column prop="username" label="用户名" ></el-table-column>,
      <el-table-column prop="phone" label="联系方式" ></el-table-column>,
      <el-table-column prop="email" label="邮箱"></el-table-column>,
      <el-table-column prop="createtime" label="创建时间"></el-table-column>,
      <el-table-column prop="updatetime" label="更新时间"></el-table-column>,
      <!--增加编辑和删除按钮-->
      <el-table-column label="操作" width="320">
        <template v-slot="scope">
          <el-button type="primary" @click="$router.push('/editAdmin?id=' + scope.row.id)">编辑</el-button>
          <el-popconfirm
              title="确定要删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button slot="reference" type="danger" style="margin-left: 5px">删除</el-button>
          </el-popconfirm>
          <el-button type="warning" style="margin-left: 5px" @click="handleChangepass(scope.row)">修改密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--element-ui分页组件,page-size指定每一页有几条数据-->
    <div style="margin-top: 20px">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-width="100px" ref="fromRef" :rules="rules">
        <el-form-item label="新密码" prop="newpass">
          <el-input v-model="form.newpass" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="newpass">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src

import request from "@/utils/request";
import Cookies from 'js-cookie'
export default {
  name:'AdminList',
  data() {
    return {
      /*假数据*/
      tableData: [],
      total: 0,
      admin: Cookies.get('admin')? JSON.parse(Cookies.get('admin')): {},
      form: {},
      dialogFormVisible: false,
      params: {
        pageNum: 1,
        pageSize: 10,
        id: '',
        username: '',
        phone: '',
        email: ''
      },
      rules: {
        newpass: [
          {required: true, message: '请输入用户名姓名', trigger: 'blur'},
          {min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      /*axios封装request,与后端返回结果进行交互*/
      request.get('/admin/page',{params: this.params}).then(res => {
        if(res.code === '200') {
          console.log(res.data.list)
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
        name: '',
        phone: '',
        email: ''
      }
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.load();
    },
    newpass() {
      this.$refs['fromRef'].validate((valid) => {
        if (valid) {
          request.put('/admin/password', this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('修改成功');
              if (this.form.id === this.admin.id) { //修改的用户ID等于当前登录的管理员的Id
                Cookies.remove('admin')
                this.$router.push('/login')
              }
              else {
                this.dialogFormVisible = false
                this.load();
                this.$notify.success('修改成功')
              }
            }else {
              this.$notify.error('修改失败')
            }
          })
        }
      })
    },
    handleChangepass(row) {
      this.dialogFormVisible = true
      this.form = JSON.parse(JSON.stringify(row))
    },
    del(id) {
      request.delete('/admin/delete/' + id).then(res => {
        if (res.code === '200') {
          this.$notify.success('删除成功');
          this.load()
        }
        else {
          this.$notify.error('删除失败')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>